package com.tiemei.demo.unittest.mockito;
import static org.mockito.Mockito.*;

import java.util.LinkedList;

import org.junit.Test;

public class StubToLetMockObjReturnExceptedValue {
	
	/*
	 * attention:
	 * 1. 类也能被mock
	 * 2. 可验证mock obj某一方法被调用次数
	 */
	@Test
	public void test(){
		// 1. mock
		// You can mock concrete classes, not only interfaces  
		LinkedList mockedList = mock(LinkedList.class);  
		  
		// stubbing  
		when(mockedList.get(0)).thenReturn("first");  
		when(mockedList.get(1)).thenThrow(new RuntimeException());  
		  
		// 2. run
		// following prints "first"  
		System.out.println(mockedList.get(0));  
		// following throws runtime exception  
		System.out.println(mockedList.get(1));  
		// following prints "null" because get(999) was not stubbed  
		System.out.println(mockedList.get(999));  
		  
		// 3. verify
		// Although it is possible to verify a stubbed invocation, usually it's just redundant  
		// See http://monkeyisland.pl/2008/04/26/asking-and-telling  
		verify(mockedList, atLeast(2)).get(0);  
	}
}
